// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

 // 引入网络模块
//import { network } from "cc";

@ccclass
export default class nextSentence extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    @property
    content1: string = 'hello';

    @property
    content2: string = 'hello';

    @property({
        type:cc.RichText
    })
    richText : cc.RichText = null

    @property({
        type:cc.RichText
    })
    richText2 : cc.RichText = null
    // LIFE-CYCLE CALLBACKS:

    // @property({
    //     type:cc.SpriteFrame
    // })
    // picSprite : cc.SpriteFrame = null
    // onLoad () {}

    start () {
        this.click();
    }

    onEnable() {

    }
 
    // 发起HTTP GET请求
    httpGetRequest(url: string) {
        //  network.httpRequest(url, (error, response) => {
        //     if (error) {
        //         cc.error(`请求出错: ${error}`);
        //     } else {
        //         cc.log(`收到响应: ${response}`);
        //     }
        // });
    }
 
    // 调用函数发起请求
    //httpGetRequest("https://your-api-endpoint.com/data");

    click() {
        console.log("---click---")
        this.label.string = "xxxxxxxxxxxxxxxxx";
        cc.loader.load('https://api.vvhan.com/api/dailyEnglish', function (err, tex) {
            console.log('Should load a texture from external url: ' + (tex));
            console.log(typeof(tex))
            if(err) {
                cc.error(err.message || err);
                return;
            }
            let jsonObj = JSON.parse(tex)
            // cc.loader.load(tex, function (err1, obj) {
            //     if (err1) {
            //         cc.error(err1.message || err1);
            //         return;
            //     }
                
            // 使用解析后的数据
            // cc.log('Name:', obj.name);
            // cc.log('Version:', obj.version);
            // cc.log('Features:', obj.features);
            if(jsonObj.success)
            { 
                //console.log(jsonObj.data.en)
                //console.log(typeof(jsonObj.data.en))
                this.text = jsonObj.data.zh;
                this.content1 = jsonObj.data.en;// "<color=#00ff00>Rich</c><color=#0fffff>Text</color>";
                this.content2 = jsonObj.data.zh;
                this.richText.string = jsonObj.data.en;
                this.richText2.string = jsonObj.data.zh;
               // this.picSprite.default = jsonObj.data.pic;

                cc.loader.load(jsonObj.data.pic, function (err2, texture) {
                    if (err2) {
                    cc.error(err.message || err2);
                    return;
                    }
                    // 创建SpriteFrame
                    var spriteFrame = new cc.SpriteFrame(texture);
                    // 设置Sprite的SpriteFrame
                    //this.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
                    // spriteFrame.getTexture().width  = 400;
                    // spriteFrame.getTexture().height  = 400;

                    //this.picSprite.spriteFrame.getTexture().width = 400;
                    //this.picSprite.spriteFrame.getTexture().height = 400;
                    //this.picSprite.spriteFrame = spriteFrame
                    var pp = this.node.parent;
                    var pp1 = this.node.parent.getChildByName('cententBG')
                    //var pp2 = this.node.parent.getchild('cententBG')
                    //var components = pp1.getComponents();
                    var components1 = pp1._components;
                    //components1[0].spriteFrame = spriteFrame
                    components1[0].spriteFrame.width = 400;
                    components1[0].spriteFrame.height = 400;
                    components1[0].spriteFrame = spriteFrame
                    //pp.zIndex = -1;
                    this.node.zIndex = 2;
                    }.bind(this));
            }
        }.bind(this));

        this.richText.string = this.content1
        this.richText2.string = this.content2
        this.node.zIndex = 2;
    }
    
    // update (dt) {}

    protected onDestroy(): void {
        console.log("---destroy---")
    }
}
